package org.jeecg.modules.simulator.factory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.simulator.model.person.path.PersonPath;
import org.jeecg.modules.simulator.model.person.path.PersonPathItem;
import org.jeecg.modules.simulator.model.person.path.PpathN;
import lombok.Getter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: gp2
 * @description:
 * @author: renzhg
 * @create: 2024-09-11 15:10
 **/
public class PersonPathJsonFactory  implements PersonPathFactory{



    @Getter
    Map<String, PersonPath> personPathMap;
    public void loadData(){
        String personpath="D:/aaaa/personpath.json";
        personPathMap=initPersonPath(personpath);
    }


    //人的路径生成
    public Map<String, PersonPath> initPersonPath(String personpath) {
        Map<String, PersonPath> personPathMap = new HashMap<>();
        List<PpathN> ppathNList = readStationBs(personpath);
//        int key=1;
        List<PersonPathItem> personPathItems = new ArrayList<>();
        for (int i = 0; i < ppathNList.size(); i++) {
            PersonPathItem personPathItem = new PersonPathItem();
            PpathN ppathN = ppathNList.get(i);
            personPathItem.setIndex(ppathN.getIndex());
            personPathItem.setStationName(ppathN.getStationName());
            personPathItem.setNeedTransfer(ppathN.isNeedTransfer());
            personPathItem.setTransfer(ppathN.isTransfer());
            personPathItem.setPathIndex(ppathN.getPathIndex());
            personPathItems.add(personPathItem);
        }
Map<String,List<PersonPathItem>>  ppiList=  personPathItems.stream()
                .collect(Collectors.groupingBy(PersonPathItem::getPathIndex));
        ppiList.forEach((k,v)->{
            Collections.sort(v);
            PersonPath path = new PersonPath(v);
            personPathMap.put(k+"_"+ v.get(0).getStationName(), path);
        });


//        PersonPath path = new PersonPath(personPathItems);
//        personPathMap.put("1", path);
        return personPathMap;

    }
    public List<PpathN> readStationBs(String path){
        List<PpathN> ppathNSList=new ArrayList<>();

        try {
            String jsonString = new String(Files.readAllBytes(Paths.get(path)));
            JSONArray jsonArray = JSON.parseArray(jsonString);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                PpathN  ppathN=new PpathN();
                String stationName = jsonObject.getString("stationName");
                ppathN.setStationName(stationName);

                String pathIndex = jsonObject.getString("pathIndex");
                ppathN.setPathIndex(pathIndex);
                boolean isTransfer = jsonObject.getBooleanValue("isTransfer");
                ppathN.setTransfer(isTransfer);
                boolean isNeedTransfer = jsonObject.getBooleanValue("isNeedTransfer");
                ppathN.setNeedTransfer(isNeedTransfer);
                int index = jsonObject.getIntValue("index");
                ppathN.setIndex(index);
                ppathNSList.add(ppathN);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ppathNSList;


    }
}
